package com.farm.quartz.controller;

import com.farm.common.core.domain.AjaxResult;
import com.farm.quartz.Dao.ProductDao;
import com.farm.quartz.domain.PlantData;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/showLine")
public class ShowLine {

    @Resource
    ProductDao productDao;

    @GetMapping("/barData")
    public AjaxResult barData() {
        System.out.println("/showLine/barData  被访问了");
        String title = "title...";
        // String[] xAxis = {"x01", "x02", "x03"};
        // int[] series = {10, 20, 30};

        PlantData plantData = new PlantData();
        plantData.setTitle(title);
        List<String> xAxis = new ArrayList<>();
        List<Integer> series = new ArrayList<>();
        List<Object[]> list = productDao.getGroupByType();
        for (Object[] i : list) {
            // System.out.println(Integer.parseInt(i[1].toString())+1);
            xAxis.add(i[0].toString());
            series.add(Integer.parseInt(i[1].toString()));
        }

        // plantData.setxAxis((String[]) xAxis.toArray());
        // plantData.setSeries((Integer[]) series.toArray());
        String[] xAxis2 = new String[xAxis.size()];
        for (int i = 0; i < xAxis2.length; i++) {
            xAxis2[i] = xAxis.get(i);
        }

        Integer[] series2 = new Integer[series.size()];
        for (int i = 0; i < series2.length; i++) {
            series2[i] = series.get(i);
        }
        plantData.setxAxis(xAxis2);
        plantData.setSeries(series2);

        return AjaxResult.success("success!", plantData);
    }

}
